package com.wy.allthingdone.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;

import com.wy.allthingdone.utils.LocalDisplay;


public class NewTextView extends TextView {
    private Paint paint;
    private String num = null;
    private CharSequence text = null;

    public NewTextView(Context context) {
        super(context);
        paint = new Paint();
        LocalDisplay.init(context);
    }

    public NewTextView(Context context, String num) {
        super(context);

    }

    public NewTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (num != null && text != null) {
            paint.setColor(Color.parseColor("#ff0000"));
            paint.setAntiAlias(true);
            canvas.drawCircle(getWidth() - LocalDisplay.dp2px(6), LocalDisplay.dp2px(6), LocalDisplay.dp2px(9), paint);
            // paint.setColor(Color.GREEN);
            paint.setColor(Color.parseColor("#42A62A"));
            paint.setTextSize(LocalDisplay.dp2px(7));
            // Log.i("TAG","num.length()===================="+ num.length());
            if (num.length() == 2) {
                canvas.drawText(num, getWidth() - LocalDisplay.dp2px(10), LocalDisplay.dp2px(9), paint);
            } else if (num.equals("null")) {
                canvas.drawText("0", getWidth() - LocalDisplay.dp2px(8), LocalDisplay.dp2px(9), paint);
            } else if (num.length() == 3) {
                canvas.drawText(num, getWidth() - LocalDisplay.dp2px(14), LocalDisplay.dp2px(9), paint);
            } else if (num.length() > 3) {
                canvas.drawText("999+", getWidth() - LocalDisplay.dp2px(16), LocalDisplay.dp2px(9), paint);
            } else {
                canvas.drawText(num, getWidth() - LocalDisplay.dp2px(8), LocalDisplay.dp2px(9), paint);
            }

        } else if (num != null) {
            paint.setColor(Color.parseColor("#ff0000"));
            paint.setAntiAlias(true);
            canvas.drawCircle(getWidth() - LocalDisplay.dp2px(6), LocalDisplay.dp2px(6), LocalDisplay.dp2px(9), paint);
            paint.setColor(Color.WHITE);
            paint.setTextSize(LocalDisplay.dp2px(7));
            // Log.i("TAG","num.length()===================="+ num.length());
            if (num.length() == 2) {
                canvas.drawText(num, getWidth() - LocalDisplay.dp2px(10), LocalDisplay.dp2px(9), paint);
            } else if (num.length() == 3) {
                canvas.drawText(num, getWidth() - LocalDisplay.dp2px(14), LocalDisplay.dp2px(9), paint);
            } else if (num.length() > 3) {
                canvas.drawText("999+", getWidth() - LocalDisplay.dp2px(16), LocalDisplay.dp2px(9), paint);
            } else {
                canvas.drawText(num, getWidth() - LocalDisplay.dp2px(8), LocalDisplay.dp2px(9), paint);
            }
        }

    }

    public void setText(CharSequence text, BufferType type, String num) {
        this.text = text;
        if (text == null) {
            this.num = num;
            paint = new Paint();
        } else {
            super.setText(text, type);
            this.num = num;
            paint = new Paint();
        }
    }
}
